//
//  HomeProListTableViewCell.m
//  ATMCash
//
//  Created by zhengchengfeng on 2024/9/7.
//

//#import "HomeProListTableViewCell.h"
//
//@implementation HomeProListTableViewCell
//
//- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
//    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
//    if (self) {
//
//        self.backgroundColor = RGB(5, 43, 9);
//
//        self.imgV = [[UIImageView alloc] init];
//        [self.contentView addSubview:self.imgV];
//        self.imgV.backgroundColor = [UIColor clearColor];
//        [self.imgV mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.contentView.mas_left).offset(15);
//            make.right.equalTo(self.contentView.mas_right).offset(-15);
//            make.top.equalTo(self.contentView.mas_top);
//            make.bottom.equalTo(self.contentView.mas_bottom);
//        }];
//        self.imgV.image = [UIImage imageNamed:@"icon_home2"];
//
//        self.backV = [[UIView alloc] init];
//        [self.imgV addSubview:self.backV];
//        self.backV.backgroundColor = [UIColor whiteColor];
//        [self.backV mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.imgV.mas_left).offset(15);
//            make.right.equalTo(self.imgV.mas_right).offset(-15);
//            make.top.equalTo(self.imgV.mas_top);
//            make.bottom.equalTo(self.imgV.mas_bottom).offset(-12);
//        }];
//        self.backV.layer.cornerRadius = 12;
//        self.backV.layer.masksToBounds = YES;
//
//        self.logoV = [[UIImageView alloc] init];
//        [self.backV addSubview:self.logoV];
//        [self.logoV mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.backV.mas_left).offset(15);
//            make.top.equalTo(self.backV.mas_top).offset(15);
//            make.width.with.height.equalTo(@24);
//        }];
//        self.logoV.image = [UIImage imageNamed:@"icon_logo"];
//
//        self.nameLab = [Utility createLabel:[UIFont fontWithName:kFuturaBT size:14] color:[UIColor colorWithHexString:@"#022D06"]];
//        [self.backV addSubview:self.nameLab];
//        [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.logoV.mas_right).offset(5);
//            make.height.equalTo(@20);
//            make.right.equalTo(self.backV.mas_right).offset(-140);
//            make.centerY.equalTo(self.logoV.mas_centerY);
//        }];
//
//        self.rateLab = [Utility createLabel:[UIFont fontWithName:kFutura size:12] color:[UIColor colorWithHexString:@"#00BA60"]];
//        [self.backV addSubview:self.rateLab];
//        [self.rateLab mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.equalTo(self.backV.mas_right).offset(-15);
//            make.width.equalTo(@120);
//            make.height.equalTo(@30);
//            make.centerY.equalTo(self.logoV.mas_centerY);
//        }];
//        self.rateLab.backgroundColor = [UIColor colorWithHexString:@"#F1F3F2"];
//        self.rateLab.layer.cornerRadius = 15;
//        self.rateLab.layer.masksToBounds = YES;
//        self.rateLab.textAlignment = 1;
//
//        self.lineV = [[UIView alloc] init];
//        self.lineV.backgroundColor = kLineColor;
//        [self.backV addSubview:self.lineV];
//        [self.lineV mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.logoV.mas_left);
//            make.right.equalTo(self.rateLab.mas_right);
//            make.top.equalTo(self.logoV.mas_bottom).offset(15);
//            make.height.equalTo(@1);
//        }];
//
//        self.priceLab = [Utility createLabel:[UIFont fontWithName:kFuturaBold size:20] color:[UIColor colorWithHexString:@"#022D06"]];
//        [self.backV addSubview:self.priceLab];
//        [self.priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.logoV.mas_left);
//            make.top.equalTo(self.lineV.mas_bottom);
//            make.right.equalTo(self.backV.mas_right).offset(-140);
//            make.bottom.equalTo(self.backV.mas_bottom);
//        }];
//        self.priceLab.numberOfLines = 2;
//
//
//        UIImage *imgB = [UIImage imageNamed:@"icon_btnBv1"];
//        self.takeBtn = [[UIButton alloc] init];
//        [self.takeBtn setBackgroundImage:imgB forState:UIControlStateNormal];
//        self.takeBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:13];
//        [self.takeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        [self.backV addSubview:self.takeBtn];
//        [self.takeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.width.equalTo(@90);
//            make.top.equalTo(self.lineV.mas_top).offset(17);
//            make.right.equalTo(self.backV.mas_right).offset(-15);
//            make.height.equalTo(@34);
//        }];

//    }
//    return self;
//}
//
//- (void)setListM:(HomeProductListModel *)listM{
//    _listM = listM;
//
//    [self.logoV sd_setImageWithURL:[NSURL URLWithString:esString(listM.productLogo)] placeholderImage:[UIImage imageNamed:@"icon_logo"]];
//    self.nameLab.text = esString(listM.productName);
//    self.rateLab.text = [NSString stringWithFormat:@"%@ | %@",esString(listM.loanRate),esString(listM.term)];
//    [self.takeBtn setTitle:@"Take It Now" forState:UIControlStateNormal];
//    self.priceLab.text = [NSString stringWithFormat:@"%@\n%@",esString(listM.amountMax),@"Loan Amount"];
//    [self.priceLab addAttributesArray:@[@{NSFontAttributeName:[UIFont fontWithName:kFutura size:13]},@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#638473"]}] forStringArray:@[@"Loan Amount",@"Loan Amount"]];
//}

//- (void)awakeFromNib {
//    [super awakeFromNib];
//    // Initialization code
//}
//
//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    [super setSelected:selected animated:animated];
//
//    // Configure the view for the selected state
//}
//
//@end


#import "HomeProListTableViewCell.h"
#import "HomeCollectionViewCell.h"

#define kCollCell            @"kCollCell"
#define kCollection_Header   @"kCollection_Header"
#define kCollection_Footer   @"kCollection_Footer"

@interface HomeProListTableViewCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic) UICollectionView *collectionView;
@property (nonatomic) NSMutableArray *dataSource;

@end

@implementation HomeProListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        
        self.backImgView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.backImgView];
        self.backImgView.backgroundColor = [UIColor clearColor];
        [self.backImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(15);
            make.right.equalTo(self.contentView.mas_right).offset(-15);
            make.top.equalTo(self.contentView.mas_top);
            make.bottom.equalTo(self.contentView.mas_bottom);
        }];
        self.backImgView.userInteractionEnabled = YES;
        
        UIImage *ic2 = [UIImage imageNamed:@"icon_homeMore"];
        UIImageView *bgimg = [[UIImageView alloc] init];
        [self.backImgView addSubview:bgimg];
        bgimg.image = ic2;
        bgimg.frame = CGRectMake((self.backImgView.size.width - ic2.size.width)/2, (self.backImgView.size.height - ic2.size.height)/2, ic2.size.width, ic2.size.height);
        [bgimg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(ic2.size.width));
            make.height.equalTo(@(ic2.size.height));
            make.top.equalTo(self.backImgView.mas_top).offset(18);
            make.left.equalTo(self.backImgView.mas_left).offset((kScreenWidth - 30 - ic2.size.width)/2);
        }];
        
        
        [self.backImgView addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backImgView.mas_left);
            make.right.equalTo(self.backImgView.mas_right);
            make.top.equalTo(self.backImgView.mas_top).offset(38);
            make.bottom.equalTo(self.backImgView.mas_bottom).offset(-25);
        }];
    }
    return self;
}

- (void)reloadData:(NSArray *)arr{
   
    
    [self.dataSource removeAllObjects];
    
    UIImage *backImg = [UIImage imageNamed:@"icon_homeCell"];
    
    self.backImgView.image = [backImg stretchableImageWithLeftCapWidth:backImg.size.width/2 topCapHeight:backImg.size.height/2];
    
    self.dataSource = [HomeProductListModel mj_objectArrayWithKeyValuesArray:arr];
    
    
    [self.collectionView reloadData];
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.sectionInset = UIEdgeInsetsZero;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        _collectionView.scrollEnabled = NO;
        
        [_collectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:kCollCell];
        [_collectionView registerClass:[UICollectionReusableView class]
            forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollection_Header];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollection_Footer];
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource{
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    kWeakSelf
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollCell forIndexPath:indexPath];
    cell.listM = self.dataSource[indexPath.item];
    cell.clickBtnBlock = ^(HomeProductListModel * _Nonnull mm) {
        if (weakSelf.callBackOrderIdStr) {
            weakSelf.callBackOrderIdStr(esString(mm.girded));
        }
    };
    return cell;
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView * reusableView;
    if (kind == UICollectionElementKindSectionHeader) {
        reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollection_Header forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor clearColor];
        for (UIView *subV in reusableView.subviews) {
            [subV removeFromSuperview];
        }
    }
    else{
        reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollection_Footer forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor clearColor];
    }
    return reusableView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kScreenWidth - 30, 136);
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kScreenWidth, 0.00001f);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(kScreenWidth, 0.00001f);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeCollectionViewCell *selCell = (HomeCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    HomeProductListModel *mm = selCell.listM;
    if (_callBackOrderIdStr) {
        _callBackOrderIdStr(esString(mm.girded));
    } 
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
